package com.jd.service.impl;

import com.gavel.parser.MYMROSkuParser;
import com.gavel.parser.SkuItem;
import com.gavel.parser.SkuParser;
import com.gavel.utils.StringUtils;
import com.jd.service.JDPictureService;
import com.jingsu.ImageLoader;
import com.jingsu.MymroInpaint;
import com.mrobuy.jd.pic.PicType;
import com.mrobuy.jdvc.dao.VCItemDao;
import com.mrobuy.jdvc.dao.VCItemPicDao;
import com.mrobuy.jdvc.entity.VCItem;
import com.mrobuy.jdvc.entity.VCItemPic;
import com.mrobuy.jdvc.service.HtmlLodService;
import com.mrobuy.jdvc.service.ProductImageService;
import com.mrobuy.util.CodeEncoder;
import com.mrobuy.util.HttpUtils;
import com.mrobuy.util.IdentifierGen;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 *
 *  京东VC后台图片相关服务
 *
 */
@Service("jdPictureService")
public class JDPictureServiceImpl implements JDPictureService {

    private static Logger LOG = LoggerFactory.getLogger(JDPictureService.class);

    @Autowired
    private JdbcTemplate jdbcTemplate;
//
    @Autowired
    private VCItemPicDao vcItemPicDao;

    @Autowired
    private VCItemDao vcItemDao;

    @Autowired
    private ProductImageService productImageService;

    @Autowired
    private HtmlLodService htmlLodService;


    @Override
    public List<String> getPictures(String skuId) {

        Set<String> picSet = new HashSet<>();
        List<VCItemPic>  pics = vcItemPicDao.load(skuId, PicType.PIC.getType());
        if ( pics!=null && pics.size() > 0 ) {
            for (VCItemPic pic : pics) {
                if (StringUtils.isNotBlank(pic.getUrl()))  {
                    picSet.add(pic.getUrl().trim());
                }
            }
        }

        if ( picSet.size() > 0 ) {
            return new ArrayList<>(picSet);
        }

        VCItem vcItem = vcItemDao.loadBySkuId(skuId);
        // 如果没有查询到记录， 生成
        if ( pics==null || pics.size()==0 ) {
            VCItemPic itemPic = new VCItemPic();
            itemPic.setId(IdentifierGen.uuid());
            itemPic.setWareId(vcItem.getWareId());
            itemPic.setSkuId(vcItem.getSkuId());
            itemPic.setSkuName(vcItem.getSkuName());
            itemPic.setType(PicType.PIC.getType());
            vcItemDao.insert(itemPic);
            pics.add(itemPic);
        }



        try {
            String code = CodeEncoder.decoder(vcItem.getItemNum());

            for (int i = 0; i < 10; i++) {
                String filename = "MYMRO_" + code + "_" + i + ".jpg";

                File existImageFile = new File("/home/images/upload/pic", filename);
                if ( !existImageFile.exists() ) {
                    continue;
                }

                try {
                    String picUrl = productImageService.upload(vcItem.getVendor(), existImageFile.getAbsolutePath(), existImageFile.getName());
                    pics.get(0).setUrl(picUrl);
                    vcItemPicDao.update(pics.get(0));
                    picSet.add(picUrl);
                    break;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return new ArrayList<>(picSet);
    }

    @Override
    public List<String> getLucencyPictures(String skuId) {
        Set<String> picSet = new HashSet<>();
        List<VCItemPic>  pics = vcItemPicDao.load(skuId, PicType.LUCENCY_PIC.getType());
        if ( pics!=null && pics.size() > 0 ) {
            for (VCItemPic pic : pics) {
                if (StringUtils.isNotBlank(pic.getUrl()))  {
                    picSet.add(pic.getUrl().trim());
                }
            }
        }

        if ( picSet.size() > 0 ) {
            return new ArrayList<>(picSet);
        }

        VCItem vcItem = vcItemDao.loadBySkuId(skuId);
        // 如果没有查询到记录， 生成
        if ( pics==null || pics.size()==0 ) {
            VCItemPic itemPic = new VCItemPic();
            itemPic.setId(IdentifierGen.uuid());
            itemPic.setWareId(vcItem.getWareId());
            itemPic.setSkuId(vcItem.getSkuId());
            itemPic.setSkuName(vcItem.getSkuName());
            itemPic.setType(PicType.LUCENCY_PIC.getType());
            vcItemDao.insert(itemPic);
            pics.add(itemPic);
        }



        try {
            String code = CodeEncoder.decoder(vcItem.getItemNum());

            for (int i = 0; i < 10; i++) {
                String filename = "MYMRO_" + code + "_" + i + ".png";

                File existImageFile = new File("/home/images/upload/lucency", filename);
                if ( !existImageFile.exists() ) {
                    continue;
                }

                try {
                    String picUrl = productImageService.upload(vcItem.getVendor(), existImageFile.getAbsolutePath(), existImageFile.getName());
                    pics.get(0).setUrl(picUrl);
                    vcItemPicDao.update(pics.get(0));
                    picSet.add(picUrl);
                    break;
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return new ArrayList<>(picSet);
    }
}
